﻿using AopCommon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Mat = Emgu.CV.Mat;
using VideoCapture = Emgu.CV.VideoCapture;
using Bitmap = System.Drawing.Bitmap;
using ReactiveUI;

namespace SharedProject2
{
    public class EmguCameraHelp
    {
        /// <summary>
        /// 相机IP
        /// </summary>
        public string IP;

        public string UserName = "admin";

        /// <summary>
        /// 是否需要发送数据
        /// </summary>
        public bool IsSendData = false;

        /// <summary>
        /// 是否显示接茬图像
        /// </summary>
        public static bool EmguCameraHelp_IsShowVideo { get; set; } = false;

        /// <summary>
        /// 相机密码
        /// </summary>
        public string Password;

        private VideoCapture capture;

        private bool m_HaveInit = false;

        static EmguCameraHelp()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"XNetCamera";
            //DllImportHelp.AddRunEnvironment(path);
            //if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            //{
            //    DllImportHelp.AddEnvironmentPaths(path);
            //}
            //Linux 暂时只能放在根目录
        }


        public EmguCameraHelp(string ip = "192.168.1.64", string password = "zckj888888")
        {
            IP = ip;
            Password = password;



            // Init();
        }

        public byte[] ImageConvertGPJ()
        {
            byte[] bytes = File.ReadAllBytes(@"D:\自己代码\接拆\0.bmp");
            MemoryStream stream = new MemoryStream(bytes);
            Bitmap bitmap = new Bitmap(stream);
            var ImageEmgu = BitmapExtension.ToImage<Bgr, UInt16>(bitmap);
            Mat image = ImageEmgu.Mat;

            using (VectorOfByte buf = new VectorOfByte())
            {
                string ext = ".jpg";
                //ext = ".png";
                CvInvoke.Imencode(ext, image, buf, null);
                byte[] array = buf.ToArray();
                return array;
            }

        }


        public void Init()
        {

            //try
            //{
            //    SXSDKInitParam dd = new SXSDKInitParam();

            //    XNetSDK.XSDK_Init(ref dd);
            //}
            //catch (Exception e)
            //{

            //}



            if (m_HaveInit)
            {
                return;
            }

            try
            {
                string video_url = $"rtsp://{UserName}:{Password}@{IP}:554/h264/ch1/sub/av_stream";

                try
                {
                    //rtsp://admin:zckj888888@192.168.1.62:554/h264/ch1/sub/av_stream
                    // capture = new VideoCapture(video_url);

                    // capture.Set(CapProp.FrameHeight, 300);
                }
                catch (Exception e)
                {

                }

                capture.ImageGrabbed += Capture_ImageGrabbed;

                if (capture.IsOpened)
                {
                    m_HaveInit = true;
                    MessageBus.Current.SendMessage<PromptMsg>($"相机{IP}初始化成功");
                    capture.Grab(); //抓取框架
                    capture.Start(); //开启一个线程抓取图像
                }
                else
                {
                    MessageBus.Current.SendMessage<PromptMsg>($"相机{IP}初始化失败");
                }


            }
            catch (Exception e)
            {
                ;

            }
        }

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {
                if (IsSendData)
                {
                    Thread.Sleep(500);
                    return;
                }
                Mat image = new Mat();
                capture.Retrieve(image);
                var cs = image.GetData();
                Byte[,,] data = cs as Byte[,,];
                //byte[] bytes = YUVtoRGB.MatToBmp(data, (int)Math.Floor(image.Width * 1.0), (int)Math.Floor(image.Height * 1.0));
                ////推送图片数据
                //byte[] result;
                //using (MemoryStream memoryStream = new MemoryStream(bytes))
                //{
                //    Bitmap bitmap = new Bitmap(memoryStream);
                //    var cr = KiResizeImage(bitmap, 500, 400); 
                //    result =  ByteArrayZip.GZipBytes(BitmapToByte(cr));
                //}
                // MessageAggregator<ImageBmpBytesMsg>.Publish(result, MessageFlagBit.ContinueVM_SendData); //通过TCP发送给平板
               // MessageAggregator<ImageBmpBytesMsg>.Publish(bytes, IP);

            }
            catch (Exception ex)
            {


            }

            Thread.Sleep(50);
        }



    }
}
